﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XukaShop.Biz;
using XukaShop.Common;
using XukaShop.Models.Entities;

namespace XukaShop.Areas.Admin.Controllers
{
    public class BrandController : Controller
    {
        private BrandBiz _brandBiz = new BrandBiz();

        [HttpGet]
        public ActionResult Index()
        {
            List<Brand> lstBrand = new List<Brand>();
            lstBrand = _brandBiz.GetListBrands();
            return View(lstBrand);
        }

        [HttpGet]
        public ActionResult BrandDetail(int? brandId)
        {
            Brand brandEnt = new Brand();
            //If brandId has value => Edit a brand
            //If brandId is null => Create new a brand
            if (brandId.HasValue)
            {
                brandEnt = _brandBiz.GetBrandById(brandId.Value);
            }
            return View(brandEnt);
        }


        [HttpPost]
        public string CreateBrand(string brandName)
        {
            if (!string.IsNullOrEmpty(brandName))
            {
                // Check is existed Name of Brand or not
                var isExisted = _brandBiz.IsExistedBrand(brandName, false);
                if (isExisted)
                {
                    return CommonConst.EXISTED;
                }

                Brand brandEnt = new Brand();
                brandEnt = _brandBiz.ConvertToBrandObject(brandName);
                var isCreateSuccess = _brandBiz.CreateBrand(brandEnt);
                if (isCreateSuccess)
                {
                    return CommonConst.SUCCESSFUL;
                }
                return CommonConst.FAIL;
            }
            return CommonConst.FAIL;
        }

        [HttpPost]
        public string EditBrand(string brandId, string brandName)
        {
            if (!string.IsNullOrEmpty(brandId) && !string.IsNullOrEmpty(brandName))
            {
                int brandIdValue = Convert.ToInt32(brandId);

                // Check is existed Name of Brand or not
                var isExisted = _brandBiz.IsExistedBrand(brandName, true, brandIdValue);
                if (isExisted)
                {
                    return CommonConst.EXISTED;
                }

                // If not existed => Update this Brand
                Brand brandEnt = new Brand();                
                brandEnt = _brandBiz.ConvertToBrandObject(brandName, brandIdValue);
                var isUpdateSuccess = _brandBiz.EditBrand(brandEnt);
                if (isUpdateSuccess)
                {
                    return CommonConst.SUCCESSFUL;
                }
                return CommonConst.FAIL;
            }
            return CommonConst.FAIL;
        }

        [HttpPost]
        public string DeleteBrand(string brandId)
        {
            if (!string.IsNullOrEmpty(brandId))
            {
                var isDeleteSuccess = _brandBiz.DeleteBrand(Convert.ToInt32(brandId));
                if (isDeleteSuccess)
                {
                    return CommonConst.SUCCESSFUL;
                }
                return CommonConst.FAIL;
            }
            return CommonConst.FAIL;
        }

        public PartialViewResult ReloadTableBrand()
        {
            List<Brand> lstBrand = new List<Brand>();
            lstBrand = _brandBiz.GetListBrands();
            return PartialView("_BrandList", lstBrand);
        }
    }
}
